package cn.fy.appcode.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import cn.fy.appcode.db.SQLite;

import java.util.List;

public class UserContentProvider extends ContentProvider {
    private static final String TAG = "UserContentProvider";
    private static final String DB_NAME = "SQL.db";
    private static final String TABLE_USER = "user";
    private static final int USER_DIR = 0;
    private static final int USER_ITEM = 1;
    private static final String AUTHORITY = "cn.fy.provider";
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        sUriMatcher.addURI(AUTHORITY, "user", USER_DIR);
        sUriMatcher.addURI(AUTHORITY, "user/#", USER_ITEM);
    }

    private SQLite sqLite;

    public UserContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase database = sqLite.getWritableDatabase();
        int deleteRows = 0;
        switch (sUriMatcher.match(uri)) {
            case USER_DIR:
                deleteRows = database.delete(TABLE_USER, selection, selectionArgs);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case USER_ITEM:
                List<String> segments = uri.getPathSegments();
                if (segments.size() < 2) {
                    return deleteRows;
                }
                String gestureId = segments.get(1);
                deleteRows = database.delete(TABLE_USER, "id=?", new String[]{gestureId});
                getContext().getContentResolver().notifyChange(uri, null);
                break;
        }
        return deleteRows;
    }

    @Override
    public String getType(Uri uri) {
        int match = sUriMatcher.match(uri);
        switch (match) {
            // 1. 必须以vnd开头
            // 2. 如果内容URI以路径结尾，则后接android.cursor.dir/，
            // 如果内容URI以id结尾，则后接android.cursor.item/
            // 3. 最后接上vnd.<authority>.<path>
            case USER_DIR:
                return "vnd.android.cursor.dir/vnd." + AUTHORITY + ".user";
            case USER_ITEM:
                return "vnd.android.cursor.item/vnd." + AUTHORITY + ".user";
            default:
                throw new IllegalArgumentException(String.format("Unknown URI: %s", uri));
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase database = sqLite.getWritableDatabase();
        Uri uriReturn = null;
        switch (sUriMatcher.match(uri)) {
            case USER_DIR:
            case USER_ITEM:
                long userId = database.insert(TABLE_USER, null, values);
                uriReturn = Uri.parse("content://" + AUTHORITY + "/user/" + userId);
                getContext().getContentResolver().notifyChange(uriReturn, null);
                break;
        }
        return uriReturn;
    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        if (context == null) {
            return false;
        }
        sqLite = new SQLite(context);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // 查询数据
        SQLiteDatabase database = sqLite.getReadableDatabase();
        Cursor cursor = null;
        switch (sUriMatcher.match(uri)) {
            case USER_DIR:
                cursor = database.query(TABLE_USER, projection, selection, selectionArgs,
                        null, null, sortOrder);
                cursor.setNotificationUri(getContext().getContentResolver(), uri);
                break;
            case USER_ITEM:
                List<String> segments = uri.getPathSegments();
                if (segments.size() < 2) {
                    return null;
                }
                String gestureId = segments.get(1);
                cursor = database.query(TABLE_USER, projection, "id=?", new String[]{gestureId},
                        null, null, sortOrder);
                cursor.setNotificationUri(getContext().getContentResolver(), uri);
                break;
        }
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        SQLiteDatabase database = sqLite.getWritableDatabase();
        int updateRows = 0;
        switch (sUriMatcher.match(uri)) {
            case USER_DIR:
                updateRows = database.update(TABLE_USER, values, selection, selectionArgs);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case USER_ITEM:
                List<String> segments = uri.getPathSegments();
                if (segments.size() < 2) {
                    return updateRows;
                }
                String gestureId = segments.get(1);
                updateRows = database.update(TABLE_USER, values, "id=?", new String[]{gestureId});
                getContext().getContentResolver().notifyChange(uri, null);
                break;
        }
        return updateRows;
    }
}